<%--
  Created by IntelliJ IDEA.
  User: LXJ
  Date: 2021/6/17
  Time: 14:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>汽车管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/component/pear/css/pear.css"/>
</head>
<%--添加新的模板--%>
<script type="text/html" id="addTpl">
    <form class="layui-form" action="">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">车牌号</label>
                        <div class="layui-input-block">
                            <input type="text" name="num" lay-verify="title" autocomplete="off"
                                   placeholder="请输入车牌号" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">类型</label>
                        <div class="layui-input-block">
                            <select name="type" lay-search>
                                <option value=""></option>
                                <option value="1">轿车</option>
                                <option value="2">SUV</option>
                                <option value="3">跑车</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">颜色</label>
                        <div class="layui-input-block" style="width: 40px">
                            <input type="color" name="color" lay-verify="title" autocomplete="off"
                                   placeholder="请输入颜色" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">价格</label>
                        <div class="layui-input-block">
                            <input type="text" name="price" lay-verify="title" autocomplete="off" placeholder="请输入价格"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">出租价格</label>
                        <div class="layui-input-block">
                            <input type="text" name="rentPrice" lay-verify="title" autocomplete="off" placeholder="请输入出租价格"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">押金</label>
                        <div class="layui-input-block">
                            <input type="text" name="deposit" lay-verify="title" autocomplete="off" placeholder="请输入押金"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">描述</label>
                        <div class="layui-input-block">
                            <textarea name="descp" placeholder="请输入描述" class="layui-textarea"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">图片</label>
                        <div class="layui-input-block">
                            <input type="text" name="img" lay-verify="title" autocomplete="off" placeholder="请输入押金"
                                   class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                        lay-filter="car-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</script>
<%--添加的修改的模板--%>
<script type="text/html" id="updateTpl">
    <form class="layui-form" action="" lay-filter="update-car">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">车牌号</label>
                        <div class="layui-input-block">
                            <input name="id" hidden>
                            <input type="text" name="num" lay-verify="title" autocomplete="off"
                                   placeholder="请输入车牌号" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">类型</label>
                        <div class="layui-input-block">
                            <select name="type" lay-search>
                                <option value=""></option>
                                <option value="1">轿车</option>
                                <option value="2">SUV</option>
                                <option value="3">跑车</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">颜色</label>
                        <div class="layui-input-block" style="width: 40px">
                            <input type="color" name="color" lay-verify="title" autocomplete="off"
                                   placeholder="请输入颜色" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">价格</label>
                        <div class="layui-input-block">
                            <input type="text" name="price" lay-verify="title" autocomplete="off" placeholder="请输入价格"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">出租价格</label>
                        <div class="layui-input-block">
                            <input type="text" name="rentPrice" lay-verify="title" autocomplete="off" placeholder="请输入出租价格"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">押金</label>
                        <div class="layui-input-block">
                            <input type="text" name="deposit" lay-verify="title" autocomplete="off" placeholder="请输入押金"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">描述</label>
                        <div class="layui-input-block">
                            <textarea name="descp" placeholder="请输入描述" class="layui-textarea"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">图片</label>
                        <div class="layui-input-block">
                            <input type="text" name="img" lay-verify="title" autocomplete="off" placeholder="请输入押金"
                                   class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                        lay-filter="car-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</script>
<%--添加一个租车的模板--%>
<script type="text/html" id="rentTpl">
    <form class="layui-form" action="" lay-filter="rent-car">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">车牌号</label>
                        <div class="layui-input-block">
                            <input name="id" hidden>
                            <input type="text" name="num" lay-verify="title" autocomplete="off"
                                   placeholder="请输入车牌号" class="layui-input layui-disabled" readonly>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">出租价格</label>
                        <div class="layui-input-block">
                            <input type="text" name="rentPrice" lay-verify="title" autocomplete="off" placeholder="请输入出租价格"
                                   class="layui-input layui-disabled" readonly>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">押金</label>
                        <div class="layui-input-block">
                            <input type="text" name="deposit" lay-verify="title" autocomplete="off" placeholder="请输入押金"
                                   class="layui-input layui-disabled" readonly>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">证件号码</label>
                        <div class="layui-input-block">
                            <input type="text" name="idCard" lay-verify="title" autocomplete="off" placeholder="请输入客户的证件号码"
                                   class="layui-input" >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">日期范围</label>
                        <div class="layui-inline" id="dataScope" >
                            <div class="layui-input-inline" >
                                <input type="text" autocomplete="off" name="startTime" id="startTime" class="layui-input" placeholder="开始日期">
                            </div>
                            <div class="layui-form-mid" >-</div>
                            <div class="layui-input-inline">
                                <input type="text" autocomplete="off" name="endTime" id="endTime" class="layui-input" placeholder="结束日期">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                        lay-filter="car-rent">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</script>

<body class="pear-container">
<%--搜索--%>
<div class="layui-card">
    <div class="layui-card-body ">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <form class="layui-form" action="" onsubmit="return false">
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">车牌号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="num" required placeholder="请输入输入框内容" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">类型</label>
                        <div class="layui-input-inline">
                            <select name="type" lay-search>
                                <option value="">请选择</option>
                                <option value="1">轿车</option>
                                <option value="2">SUV</option>
                                <option value="3">跑车</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">价格范围</label>
                        <div class="layui-input-inline">
                            <input type="text" name="minPrice" required placeholder="最低价" autocomplete="off"
                                   class="layui-input">
                            <input type="text" name="maxPrice" required placeholder="最高价格" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">是否出租</label>
                        <div class="layui-input-inline">
                            <select name="isRent" lay-search>
                                <option value=""></option>
                                <option value="0">未出租</option>
                                <option value="1">已出租</option>
                            </select>
                        </div>
                    </div>
                    <hr/>
                    <div class="layui-form-item layui-input-block" align="center">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="car-query">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                    </div>
                </form>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="car-table" lay-filter="car-table"></table>
    </div>
</div>

<script type="text/html" id="car-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="car-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
    <button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="rent"><i
            class="layui-icon layui-icon-add-circle"></i>
    </button>
</script>

<script type="text/html" id="car-type">
<%--    类型: 1 轿车 2 SUV 3 跑车--%>
    {{#if (d.type == 1) { }}
    <span>轿车</span>
    {{# }else if(d.type == 2){ }}
    <span>SUV</span>
   {{# }else if(d.type == 3){ }}
   <span>跑车</span>
    {{# } }}
</script>

<script type="text/html" id="car-isRent">
    <input type="checkbox" car-id="{{d.id}}" name="enable" lay-skin="switch" lay-text="借出|未借出" lay-filter="car-enable"
           {{d.isRent==1 ? 'checked': ''}}></input>
</script>

<script type="text/html" id="car-createTime">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}
</script>
<script src="${pageContext.request.contextPath}/resources/component/layui/layui.js"></script>
<script src="${pageContext.request.contextPath}/resources/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common','dtree','laydate'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let dtree = layui.dtree;
        var laydate = layui.laydate;
        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '车牌号',
                    field: 'num',
                    align: 'center',
                    width: 100
                },
                {
                    title: '类型',
                    field: 'type',
                    align: 'center',
                    templet: '#car-type',
                    width: 60,
                },
                {
                    title: '颜色',
                    field: 'color',
                    align: 'center',
                    width: 120,
                },
                {
                    title: '价格',
                    field: 'price',
                    align: 'center'
                },
                {
                    title: '出租价格',
                    field: 'rentPrice',
                    align: 'center'
                },
                {
                    title: '押金',
                    field: 'deposit',
                    align: 'center'
                },
                {
                    title: '是否借出',
                    field: 'isRent',
                    align: 'center',
                    templet: '#car-isRent',
                    width: 110,
                },
                {
                    title: '描述',
                    field: 'descp',
                    align: 'center',
                    width: 120,
                },
                {
                    title: '图片',
                    field: 'img',
                    align: 'center',
                },
                {
                    title: '创建时间',
                    field: 'createTime',
                    align: 'center',
                    templet: '#car-createTime',
                    width: 120,
                },
                {
                    title: '操作',
                    toolbar: '#car-bar',
                    align: 'center',
                    width: 150
                }
            ]
        ]

        //Table 数据的加载
        table.render({
            elem: '#car-table',
            url: '${pageContext.request.contextPath}/buscar/findByPage.do',
            page: true,
            // 对象的属性和table的列对应
            cols: cols,
            skin: 'line',
            toolbar: '#car-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            // res == Result
            parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.count, //解析数据长度
                    "data": res.data.data //解析数据列表
                }
            },
            response: {
                statusCode: 200
            }
        });

        table.on('tool(car-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'rent') {
                window.rent(obj);
            }
        });

        table.on('toolbar(car-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(car-query)', function (data) {
            table.reload('car-table', {
                where: data.field
            })
            return false;
        });

        window.rent = function (obj) {
            layer.open({
                // 页面层
                type: 1,
                title: '开始租车',
                shade: 0.1,
                area: [common.isModile() ? '100%' : '500px', common.isModile() ? '100%' : '400px'],
                content: $("#rentTpl").html(),
                success: function (layerno, index) {
                    form.val('rent-car',obj.data)
                    //日期范围
                    laydate.render({
                        elem: '#dataScope'
                        //设置开始日期、日期日期的 input 选择器
                        //数组格式为 2.6.6 开始新增，之前版本直接配置 true 或任意分割字符即可
                        ,range: ['#startTime', '#endTime']
                    });
                    // 给按钮绑定事件
                    form.on('submit(car-rent)', function (data) {
                        // 发送租车的请求
                        let fieldData = data.field ;
                        console.log(fieldData)
                        let form = {
                            "carId":fieldData.id ,
                            "idCard":fieldData.idCard ,
                            "startTime":fieldData.startTime ,
                            "endTime":fieldData.endTime ,
                        }
                        $.ajax({
                            url: '${pageContext.request.contextPath}/buscar/rent.do',
                            data: form,
                            // 前端会告诉后端,我当前的值是一个json值.
                            // dataType: 'json',
                            // contentType: 'application/json',
                            type: 'post',
                            success: function (result) {
                                if (result.code == 200) {
                                    layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                        // 关闭当前的弹层
                                        layer.close(index);
                                        // 重新加载我们的表格
                                        table.reload("car-table");
                                    });
                                } else {
                                    layer.msg(result.msg, {icon: 2, time: 1000});
                                }
                            }
                        })
                        return false;
                    });
                }
            });
        }
        window.add = function () {
            layer.open({
                // 页面层
                type: 1,
                title: '新增',
                shade: 0.1,
                area: [common.isModile() ? '100%' : '600px', common.isModile() ? '100%' : '500px'],
                content: $("#addTpl").html(),
                success: function (layerno, index) {
                    // 渲染form
                    form.render()
                    // 给按钮绑定事件
                    form.on('submit(car-save)', function (data) {
                        $.ajax({
                            url: '${pageContext.request.contextPath}/buscar/add.do',
                            data: JSON.stringify(data.field),
                            dataType: 'json',
                            contentType: 'application/json',
                            type: 'post',
                            success: function (result) {
                                if (result.code == 200) {
                                    layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                        // 关闭当前的弹层
                                        layer.close(index);
                                        // 重新加载我们的表格
                                        table.reload("car-table");
                                    });
                                } else {
                                    layer.msg(result.msg, {icon: 2, time: 1000});
                                }
                            }
                        })
                        return false;
                    });
                }
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 1,
                title: '修改',
                shade: 0.1,
                area: ['600px', '500px'],
                content: $("#updateTpl").html(),
                success: function (layero, index) {
                    // 数据的回显update-car
                    form.val("update-car", obj.data);
                    form.render();
                    form.on('submit(car-save)', function (data) {
                        $.ajax({
                            url: '${pageContext.request.contextPath}/buscar/update.do',
                            data: JSON.stringify(data.field),
                            dataType: 'json',
                            contentType: 'application/json',
                            type: 'post',
                            success: function (result) {
                                if (result.code == 200) {
                                    layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                        layer.close(index);//关闭当前页
                                        table.reload("car-table");
                                    });
                                } else {
                                    layer.msg(result.msg, {icon: 2, time: 1000});
                                }
                            }
                        })
                        return false;
                    });

                }
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该用户', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "${pageContext.request.contextPath}/buscar/delete.do/" + obj.data['id'],
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {

            // 获取到你选择的数据 ,checkField 将会变成一个使用, 分割的字符串
            //id: 用户id
            var checkIds = common.checkField(obj, 'id');

            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除这些用户', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "${pageContext.request.contextPath}/buscar/delete.do/" + checkIds,
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('car-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('car-table');
        }
    })
</script>
</body>
</html>

